/**
 * 进行地址加载
 * 
 * @param type
 *            类型
 * @param obj
 *            本身
 */
function changeCityOrArea(type, obj) {
     
	var index = obj.selectedIndex;
	var parentAddressId = obj.options[obj.selectedIndex].value;
	var params = "parentAddressId=" + parentAddressId;
	var city = $("#city")[0];
	var area = $("#area")[0];
	// 进行清空
	if (index == 0) {
		switch (type) {
		case 0:
			city.options.length = 1;
			area.options.length = 1;
			break;
		case 1:
			area.options.length = 1;
			break;
		}
	} else {
		// 成功回调
		var success = function(data) {

			switch (type) {
			case 0:
				var object = city;
				city.options.length = 1;
				area.options.length = 1;				
				break;
			case 1:
				var object = area;
				area.options.length = 1;				
				break;
			}
			// 进行铺展
			for (var i = 0; i < data.length; i++) {

				cmbAddOption(object, data[i].addressName, data[i].addressId);
			}

		}

		var error = function() {
			alert("加载失败")
		}

		var url = base_url  + "customer/customerfindAddresses";
		// 进行请求
		doAjax("POST", url, params,
				success, error, true, false);
	}

}

// 添加城市或者地址想
function cmbAddOption(cmb, str, value) {
	
	// 创建一个option项
	var option = document.createElement("OPTION");
	// 添加option
	cmb.options.add(option);
	// 初始化名称
	option.innerHTML = str;

	if (value != null) {
		// 值
		option.value = value;
	}
}

function addOrUpdate(type){
	
	//获取姓名等的信息
	var id = $("#id").val();
	if(id==null || id =="" || id == undefined){
		id = null ;
	}
	var name = $("#name").val();
	var phone = $("#phone").val();
	var menpaihao = $("#menpaihao").val();	
	var flag = true ; 
	//获得地址的信息
	var oneAddressId = $("#province")[0].options[$("#province")[0].selectedIndex].value;
	var twoAddressId = $("#city")[0].options[$("#city")[0].selectedIndex].value;
	var threeAddressId = $("#area")[0].options[$("#area")[0].selectedIndex].value;

	//获得地址的内容
	var oneAddressName =$("#province")[0].options[$("#province")[0].selectedIndex].innerHTML;
	var twoAddressName = $("#city")[0].options[$("#city")[0].selectedIndex].innerHTML;
	var threeAddressName = $("#area")[0].options[$("#area")[0].selectedIndex].innerHTML;
	

	//如果是0，就设置为空
    if(twoAddressId==0){
    	twoAddressName = null ; 
    }
    
    if(threeAddressId==0){
    	threeAddressName = null ; 
    }
	
	//进行空值判断
	if(name==null || name=="" || name == undefined){	  	
		tip.content("名字不能为空").show();
		flag = false;
	}else {		
		if(phone==null || phone=="" || phone == undefined){		  	
			tip.content("电话不能为空").show();
			flag = false ;
		}else {	
			
			if(oneAddressId == 0){	
				tip.content("省份不能为空").show();
				flag = false;
			}else{		
				if(menpaihao==null || menpaihao=="" || menpaihao == undefined){		
					tip.content("门牌号不能为空").show();
					flag = false;
				}
			}
		}
	}
	
	if(flag){

		//是否是默认收获地址
		var isdefaultAddress = $("#default").hasClass("iconfont");
		var isDefault = 0 ;
		if(isdefaultAddress){
			isDefault= 1;
		}
		var json = {
				"receiptAddressId":id,
				"userId":null,
				"oneAddressId":oneAddressId,
				"oneAddressName":oneAddressName,
				"twoAddressId":twoAddressId,
				"twoAddressName":twoAddressName,
				"threeAddressId":threeAddressId,
				"threeAddressName":threeAddressName,
				"detailAddress":menpaihao,
				"contactsPerson":name,
				"contactsWay":phone,
				"isDefaultReceiptAddress":isDefault
		}		
		
		//参数
	    var params = {
				"jsonReceitAddress":JSON.stringify(json)
				}
		switch (type) {
		case 0:
        	//添加的url
    	    var url =  base_url+"customer/customeraddAddress";			
			break;
        case 1:
        	//更新的url
    	    var url =  base_url + "customer/customerUpdateReceiptAddress";
			break;

		default:
			break;
		}
		//进行请求
	    post(url, params);
	}
}
